package com.lmx.web.controller.thirdPart;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lmx.persistence.service.TenantService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;

/*-
 * 处理接口
 */

@Controller
@RequestMapping("/access/tenant")
public class TenantController {
	private static Log logger = LogFactory.getLog(TenantController.class);

	@Autowired
	private TenantService tenantService;

	@RequestMapping("/addSourceData")
	@ResponseBody
	public void bedMatWarningData(HttpServletRequest request, HttpServletResponse response) {
		// 数据处理
		String result = "";
		logger.debug("-----------------------------start addSourceData-------------------------------------");
		tenantService.addAllModeSourceData();
		BufferedOutputStream out;
		try {
			out = new BufferedOutputStream(response.getOutputStream());
			out.write("addSourceData end".getBytes());
			out.flush();
			out.close();
		} catch (Exception e) {
			logger.debug(" addSourceData error " + e);
		}
	}

	
	@RequestMapping("/resetVideo")
	@ResponseBody
	public void resetVideo(HttpServletRequest request, HttpServletResponse response) {
		String param = request.getParameter("tid");
		Long tenantId = null;
		if (param != null && !"".equals(param) && param.matches("\\d+")) {
			tenantId = Long.parseLong(param);// 获取所传tenantid
		}
		String res = "ok";
		
		tenantService.resetVideo(tenantId);
		
		BufferedOutputStream out;
		try {
			out = new BufferedOutputStream(response.getOutputStream());
			out.write(res.getBytes());
			out.flush();
			out.close();
		} catch (IOException e) {
			logger.debug("hbeacon Relation  data  error:" + e);
		}
		logger.debug("-----------------------------hbeacon Relation  data   end-------------------------------------");
	}
	
	
	
	// 获取手环按键对应关系
	@RequestMapping("/cameraData")
	@ResponseBody
	public void getCamerData(HttpServletRequest request, HttpServletResponse response) {
		// 数据处理
		logger.debug(
				"-----------------------------hbeacon Relation  data   start-------------------------------------");
		String code = request.getParameter("code");
		String res = tenantService.getCameraData(code);
		BufferedOutputStream out;
		try {
			out = new BufferedOutputStream(response.getOutputStream());
			out.write(res.getBytes());
			out.flush();
			out.close();
		} catch (IOException e) {
			logger.debug("hbeacon Relation  data  error:" + e);
		}
		logger.debug("-----------------------------hbeacon Relation  data   end-------------------------------------");
	}
}
